<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateShopCommissionOrderTable extends Migrator
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('shop_commission_order', ['comment' => '分销订单', 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci']);
        $table->addColumn('order_id', 'integer', ['limit'  =>  11, 'default' => 0, 'comment' => '订单'])
            ->addColumn(Column::tinyInteger('self_buy')->setDefault(0)->setComment('是否分销内购:0=分销订单,1=内购订单'))
            ->addColumn('order_item_id', 'integer', ['limit'  =>  11, 'default' => 0, 'comment' => '订单商品'])
            ->addColumn('buyer_id', 'integer', ['limit'  =>  11, 'default' => 0, 'comment' => '购买人'])
            ->addColumn('goods_id', 'integer', ['limit'  =>  11, 'default' => 0, 'comment' => '商品'])
            ->addColumn('agent_id', 'integer', ['limit'  =>  11, 'default' => 0, 'comment' => '分销商'])
            ->addColumn('amount', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => 0, 'comment' => '商品结算金额'])
            ->addColumn('reward_type', 'string', ['limit' => 20, 'comment' => '商品结算方式'])
            ->addColumn('reward_event', 'string', ['limit' => 20, 'comment' => '佣金结算事件'])
            ->addColumn(Column::tinyInteger('commission_order_status')->setDefault(0)->setComment('分销商业绩:-2=已扣除,-1=已取消,0=不计入,1=已计入'))
            ->addColumn(Column::tinyInteger('commission_reward_status')->setDefault(0)->setComment('佣金处理状态:-2=已退回,-1=已取消,0=未结算,1=已结算'))
            ->addColumn('commission_rules', 'text', ['null' => true, 'comment' => '执行佣金结算规则'])
            ->addColumn('commission_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '结算时间'])
            ->addColumn('create_time', 'integer', ['limit'  =>  10, 'null' => true, 'default' => null, 'comment' => '创建时间'])
            ->addColumn('update_time', 'integer', ['limit'  => 10, 'null' => true, 'default' => null, 'comment' => '更新时间'])
            ->addIndex('order_id')
            ->addIndex('order_item_id')
            ->addIndex('buyer_id')
            ->addIndex('goods_id')
            ->addIndex('agent_id')
            ->addIndex('create_time')
            ->create();
    }


    /**
     * Migrate Down.
     */
    public function down()
    {
        $table = $this->table('shop_commission_order');
        $table->drop();
    }
}
